package cn.sccdlg.springmvc.controller;

import cn.sccdlg.springmvc.domain.Account;
import cn.sccdlg.springmvc.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author RaoShuang
 * @emil 991207823@qq.com
 * @date 2020/10/23 21:26
 * 参数绑定的控制转发测试类
 */
@Controller
@RequestMapping("param")
public class ParamController {

    /**
     * 请求参数绑定入门
     * @return 请求成功页面
     */
    @RequestMapping("/testParam")
    public String testParam(String username, String password) {
        System.out.println("testParam方法 执行了...");
        System.out.println("用户名：" + username);
        System.out.println("密码：" + password);
        return "success";
    }

    /**
     * 请求参数绑定把数据封装到JavaBean的类中
     * @return
     */
    @RequestMapping("/saveAccount")
    public String saveAccount(Account account){
        System.out.println("数据封装到JavaBean的saveAccount方法执行了...");
        System.out.println(account);
        return "success";
    }

    /**
     * 自定义类型转换器
     * @param user
     * @return
     */
    @RequestMapping("/saveUser")
    public String saveUser(User user){
        System.out.println("自定义类型参数绑定 saveUser 执行了...");
        System.out.println(user);
        return "success";
    }

    /**
     * 原生的API
     * @return
     */
    @RequestMapping("/testServlet")
    public String testServlet(HttpServletRequest request, HttpServletResponse response){
        System.out.println("原生Servlet的方法执行了...");
        System.out.println(request);

        HttpSession session = request.getSession();
        System.out.println(session);

        ServletContext servletContext = session.getServletContext();
        System.out.println(servletContext);

        System.out.println(response);
        return "success";
    }
}
